<?php
/**
 * Create by chang
 * Date 2019/12/17
 * Time 16:55
 * 测试
 */
namespace app\api\controller;

use app\common\controller\Api;
use app\common\controller\Queue;
use app\common\model\AgentOrder;
use app\common\model\Commission;
use think\Db;

class Test extends Api {
	protected $noNeedLogin = ['*'];

	/**
	 * 根据用户ID直接登录（测试使用)
	 */
	public function loginById() {
		$id = $this->request->get('id');
		$this->auth->direct($id);

		$userInfo = $this->auth->getUserinfo();
		$this->success('登录成功', $userInfo);
	}

	/**
	 * 切换用户
	 */
	public function changeUser() {
		$openId = 'o-XL25ctLvcE5rxd9fnyHX2K6ylA';
		$id = $this->request->get('id');
		Db::name('user')
			->where(['smallapp_openid' => $openId])
			->update(['smallapp_openid' => $openId.'_'.time()]);

		Db::name('user')
			->where(['id' => $id])
			->update(['smallapp_openid' => $openId]);

		$this->auth->direct($id);

		$userInfo = $this->auth->getUserinfo();
		$this->success('登录成功', $userInfo);
	}


	/**
	 * 订单支付成功
	 */
	public function orderSuccess($id) {
		$model = new \app\common\model\Order();
		$no = $this->request->get('no', time());
		$res = $model->paySuccess($id, $no);
		echo $res;
	}

	/**
	 * 订单返利
	 */
	public function orderRebate($id) {
		$obj = new Queue();
		$res = $obj->orderSuccess(['id' => $id]);
		dump($res);
	}


	/**
	 * 代理支付成功
	 */
	public function agentSuccess($id) {
		$model = new AgentOrder();
		$orderRes = $model->paySuccess($id, time());
		dump($orderRes);
		dump($model->getError());
	}


	/**
	 * 更改佣金状态为已发放
	 */
	public function sendCommission($id) {
		$model = new Commission();
		$res = $model->send($id);
		dump($res);
		dump($model->getError());
	}

}
